package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.entity.SchoolEnterpriseCooperation;
import com.example.pceb.entity.SchoolAdmin;
import com.example.pceb.entity.School;
import com.example.pceb.entity.Enterprise;
import com.example.pceb.mapper.SchoolEnterpriseCooperationMapper;
import com.example.pceb.service.SchoolEnterpriseCooperationService;
import com.example.pceb.service.SchoolAdminService;
import com.example.pceb.service.SchoolService;
import com.example.pceb.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Collections;

@Service
public class SchoolEnterpriseCooperationServiceImpl implements SchoolEnterpriseCooperationService {
    private static final Logger logger = LoggerFactory.getLogger(SchoolEnterpriseCooperationServiceImpl.class);
    
    @Autowired
    private SchoolEnterpriseCooperationMapper cooperationMapper;
    @Autowired
    private SchoolAdminService schoolAdminService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private EnterpriseService enterpriseService;

    @Override
    public boolean saveCooperation(SchoolEnterpriseCooperation cooperation) {
        return cooperationMapper.insert(cooperation) > 0;
    }

    @Override
    public List<SchoolEnterpriseCooperation> listByPublisherType(String publisherType, String projectStatus) {
        QueryWrapper<SchoolEnterpriseCooperation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher_type", publisherType);
        queryWrapper.eq("project_status", projectStatus);
        return cooperationMapper.selectList(queryWrapper);
    }

    @Override
    public SchoolEnterpriseCooperation getById(Long id) {
        return cooperationMapper.selectById(id);
    }
    
    @Override
    public boolean updateCooperationInfo(SchoolEnterpriseCooperation cooperation) {
        return cooperationMapper.updateCooperationInfo(cooperation) > 0;
    }
    
    @Override
    public List<SchoolEnterpriseCooperation> getMyProjects(String username, String userType) {
        QueryWrapper<SchoolEnterpriseCooperation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher_username", username);
        if ("enterprise".equals(userType)) {
            queryWrapper.eq("publisher_type", "企业");
        } else if ("school".equals(userType)) {
            queryWrapper.eq("publisher_type", "学校");
        }
        queryWrapper.orderByDesc("id");
        return cooperationMapper.selectList(queryWrapper);
    }
    
    @Override
    public boolean deleteProject(Long id, String username) {
        QueryWrapper<SchoolEnterpriseCooperation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("publisher_username", username);
        queryWrapper.eq("project_status", "未立项");
        return cooperationMapper.delete(queryWrapper) > 0;
    }
    
    @Override
    public List<SchoolEnterpriseCooperation> getSchoolAdminPendingProjects(String username) {
        logger.info("开始查询学校管理员项目，用户名: {}", username);
        
        // 根据用户名获取学校管理员信息
        SchoolAdmin admin = schoolAdminService.getByUsername(username);
        if (admin == null) {
            logger.warn("未找到学校管理员信息，用户名: {}", username);
            return Collections.emptyList();
        }
        if (admin.getSchoolId() == null) {
            logger.warn("学校管理员未关联学校，用户名: {}, 管理员信息: {}", username, admin);
            return Collections.emptyList();
        }
        
        logger.info("找到学校管理员信息，用户名: {}, 学校ID: {}", username, admin.getSchoolId());
        
        // 根据学校ID获取学校名称
        School school = schoolService.getById(admin.getSchoolId());
        if (school == null) {
            logger.warn("未找到学校信息，学校ID: {}", admin.getSchoolId());
            return Collections.emptyList();
        }
        
        logger.info("找到学校信息，学校ID: {}, 学校名称: {}", admin.getSchoolId(), school.getSchoolName());
        
        // 查询项目状态不为"未立项"且学校名称匹配的项目
        QueryWrapper<SchoolEnterpriseCooperation> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("project_status", "未立项");
        queryWrapper.eq("school_name", school.getSchoolName());
        queryWrapper.orderByDesc("id");
        
        List<SchoolEnterpriseCooperation> result = cooperationMapper.selectList(queryWrapper);
        logger.info("查询到 {} 个项目，学校名称: {}", result.size(), school.getSchoolName());
        
        return result;
    }
    
    @Override
    public List<SchoolEnterpriseCooperation> getEnterpriseAdminPendingProjects(String username) {
        logger.info("开始查询企业管理员项目，用户名: {}", username);
        
        // 根据用户名获取企业信息
        Enterprise enterprise = enterpriseService.getByUsername(username);
        if (enterprise == null) {
            logger.warn("未找到企业信息，用户名: {}", username);
            return Collections.emptyList();
        }
        
        logger.info("找到企业信息，用户名: {}, 企业名称: {}", username, enterprise.getEnterprise());
        
        // 查询项目状态不为"未立项"且企业名称匹配的项目
        QueryWrapper<SchoolEnterpriseCooperation> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("project_status", "未立项");
        queryWrapper.eq("enterprise_name", enterprise.getEnterprise());
        queryWrapper.orderByDesc("id");
        
        List<SchoolEnterpriseCooperation> result = cooperationMapper.selectList(queryWrapper);
        logger.info("查询到 {} 个项目，企业名称: {}", result.size(), enterprise.getEnterprise());
        
        return result;
    }
}